<template>
	<div class="diy_edit page_trial_appointment" id="trial_appointment_edit">
		<div class='warp'>
			<div class='container'>
				<div class='row diy_edit_content_box'>
						<div v-if="$check_field('set','shop_name') || $check_field('add','shop_name') || $check_field('get','shop_name')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								店铺名称:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_shop_name" v-model="form['shop_name']" placeholder="请输入店铺名称" v-if="(form['shop_name'] && $check_field('set','shop_name')) || (!form['shop_name'] && $check_field('add','shop_name'))"  :disabled="disabledObj['shop_name_isDisabled']"/>
							<span v-else-if="$check_field('get','shop_name')">{{ form['shop_name'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','detailed_address') || $check_field('add','detailed_address') || $check_field('get','detailed_address')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								详细地址:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_detailed_address" v-model="form['detailed_address']" placeholder="请输入详细地址" v-if="(form['detailed_address'] && $check_field('set','detailed_address')) || (!form['detailed_address'] && $check_field('add','detailed_address'))"  :disabled="disabledObj['detailed_address_isDisabled']"/>
							<span v-else-if="$check_field('get','detailed_address')">{{ form['detailed_address'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','business_scope') || $check_field('add','business_scope') || $check_field('get','business_scope')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								经营范围:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_business_scope" v-model="form['business_scope']" placeholder="请输入经营范围" v-if="(form['business_scope'] && $check_field('set','business_scope')) || (!form['business_scope'] && $check_field('add','business_scope'))"  :disabled="disabledObj['business_scope_isDisabled']"/>
							<span v-else-if="$check_field('get','business_scope')">{{ form['business_scope'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','business_user') || $check_field('add','business_user') || $check_field('get','business_user')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								商家用户:
							</span>
						</div>
						<div class="diy_field diy_down">
							<select id="form_business_user" :disabled="disabledObj['business_user_isDisabled']" v-model="form['business_user']" v-if="(form['business_user'] && $check_field('set','business_user')) || (!form['business_user'] && $check_field('add','business_user'))" >
								<option v-for="o in list_user_business_user" :value="o['user_id']">
									{{o['nickname'] + '-' + o['username']}}
								</option>
							</select>
							<span v-else-if="$check_field('get','business_user')">{{ get_user_info("business_user", form['business_user']) }}</span>
						</div>
					</div>
							<div v-if="$check_field('set','business_name') || $check_field('add','business_name') || $check_field('get','business_name')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								商家姓名:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_business_name" v-model="form['business_name']" placeholder="请输入商家姓名" v-if="(form['business_name'] && $check_field('set','business_name')) || (!form['business_name'] && $check_field('add','business_name'))"  :disabled="disabledObj['business_name_isDisabled']"/>
							<span v-else-if="$check_field('get','business_name')">{{ form['business_name'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','business_phone') || $check_field('add','business_phone') || $check_field('get','business_phone')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								商家电话:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_business_phone" v-model="form['business_phone']" placeholder="请输入商家电话" v-if="(form['business_phone'] && $check_field('set','business_phone')) || (!form['business_phone'] && $check_field('add','business_phone'))"  :disabled="disabledObj['business_phone_isDisabled']"/>
							<span v-else-if="$check_field('get','business_phone')">{{ form['business_phone'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','member_user') || $check_field('add','member_user') || $check_field('get','member_user')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								会员用户:
							</span>
						</div>
						<div class="diy_field diy_down">
							<select id="form_member_user" :disabled="disabledObj['member_user_isDisabled']" v-model="form['member_user']" v-if="(form['member_user'] && $check_field('set','member_user')) || (!form['member_user'] && $check_field('add','member_user'))" >
								<option v-for="o in list_user_member_user" :value="o['user_id']">
									{{o['nickname'] + '-' + o['username']}}
								</option>
							</select>
							<span v-else-if="$check_field('get','member_user')">{{ get_user_info("member_user", form['member_user']) }}</span>
						</div>
					</div>
							<div v-if="$check_field('set','user_name') || $check_field('add','user_name') || $check_field('get','user_name')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								用户姓名:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_user_name" v-model="form['user_name']" placeholder="请输入用户姓名" v-if="(form['user_name'] && $check_field('set','user_name')) || (!form['user_name'] && $check_field('add','user_name'))"  :disabled="disabledObj['user_name_isDisabled']"/>
							<span v-else-if="$check_field('get','user_name')">{{ form['user_name'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','contact_information') || $check_field('add','contact_information') || $check_field('get','contact_information')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								联系方式:
							</span>
						</div>
								<!-- 文本 -->
									<div class="diy_field diy_text">
							<input type="text" id="form_contact_information" v-model="form['contact_information']" placeholder="请输入联系方式" v-if="(form['contact_information'] && $check_field('set','contact_information')) || (!form['contact_information'] && $check_field('add','contact_information'))"  :disabled="disabledObj['contact_information_isDisabled']"/>
							<span v-else-if="$check_field('get','contact_information')">{{ form['contact_information'] }}</span>
						</div>
										</div>
							<div v-if="$check_field('set','appointment_date') || $check_field('add','appointment_date') || $check_field('get','appointment_date')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								预约日期:
							</span>
						</div>
								<!-- 日期 -->
						<div class="diy_field diy_date">
							<input type="date" :disabled="disabledObj['appointment_date_isDisabled']" id="form_appointment_date" v-model="form['appointment_date']" placeholder="请输入预约日期" v-if="(form['appointment_date'] && $check_field('set','appointment_date')) || (!form['appointment_date'] && $check_field('add','appointment_date'))"/>
							<span v-else-if="$check_field('get','appointment_date')">{{ form['appointment_date'] }}</span>
						</div>
							</div>
							<div v-if="$check_field('set','appointment_status') || $check_field('add','appointment_status') || $check_field('get','appointment_status')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								预约状态:
							</span>
						</div>
								<!-- 选项 -->
						<div class="diy_field diy_down">
							<select id="form_appointment_status" v-model="form['appointment_status']" v-if="(form['appointment_status'] && $check_field('set','appointment_status')) || (!form['appointment_status'] && $check_field('add','appointment_status'))" >
								<option v-for="o in list_appointment_status" :value="o">
									{{o}}
								</option>
							</select>
							<span v-else-if="$check_field('get','appointment_status')">{{ form['appointment_status'] }}</span>
						</div>
							</div>
							<div v-if="$check_field('set','appointment_remarks') || $check_field('add','appointment_remarks') || $check_field('get','appointment_remarks')" class="form-item col-12 col-md-6">
						<div class="diy_title">
							<span>
								预约备注:
							</span>
						</div>
								<!-- 多文本 -->
						<div class="diy_field diy_desc">
							<textarea id="form_appointment_remarks" v-model="form['appointment_remarks']" v-if="(form['appointment_remarks'] && $check_field('set','appointment_remarks')) || (!form['appointment_remarks'] && $check_field('add','appointment_remarks'))" :disabled="disabledObj['appointment_remarks_isDisabled']" />
							<span v-else-if="$check_field('get','appointment_remarks')">{{ form['appointment_remarks'] }}</span>
						</div>
							</div>
	




				</div>
				<div class="diy_edit_submit_box row">
					<div class="col-12">
						<div class="btn_box">
							<button class="btn_submit" @click="submit()">提交</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
	import mixin from "@/mixins/page.js";
	export default {
		mixins: [mixin],
		components: {},
		data() {
			return {
				url_get_obj: "~/api/trial_appointment/get_obj?",
				url_add: "~/api/trial_appointment/add?",
				url_set: "~/api/trial_appointment/set?",

				// 登录权限
				oauth: {
					"signIn": true,
					"user_group": []
				},

				// 查询条件
				query: {
						"shop_name": "",
							"detailed_address": "",
							"business_scope": "",
							"business_user": 0,
							"business_name": "",
							"business_phone": "",
							"member_user": 0,
							"user_name": "",
							"contact_information": "",
							"appointment_date": "",
							"appointment_status": "",
							"appointment_remarks": "",
						"trial_appointment_id": 0,
				},

				obj: {
						"shop_name":  '', // 店铺名称
							"detailed_address":  '', // 详细地址
							"business_scope":  '', // 经营范围
							"business_user": 0, // 商家用户
							"business_name":  '', // 商家姓名
							"business_phone":  '', // 商家电话
							"member_user": 0, // 会员用户
							"user_name":  '', // 用户姓名
							"contact_information":  '', // 联系方式
							"appointment_date": new Date().getTime(),
							"appointment_status":  '', // 预约状态
							"appointment_remarks":  '', // 预约备注
						"trial_appointment_id": 0,
				},

				// 表单字段
				form: {
						"shop_name":  '', // 店铺名称
							"detailed_address":  '', // 详细地址
							"business_scope":  '', // 经营范围
							"business_user": 0, // 商家用户
							"business_name":  '', // 商家姓名
							"business_phone":  '', // 商家电话
							"member_user": 0, // 会员用户
							"user_name":  '', // 用户姓名
							"contact_information":  '', // 联系方式
							"appointment_date": new Date().getTime(),
							"appointment_status":  '', // 预约状态
							"appointment_remarks":  '', // 预约备注
						"trial_appointment_id": 0,
				},
				disabledObj:{
						"shop_name_isDisabled": false,
							"detailed_address_isDisabled": false,
							"business_scope_isDisabled": false,
							"business_user_isDisabled": false,
							"business_name_isDisabled": false,
							"business_phone_isDisabled": false,
							"member_user_isDisabled": false,
							"user_name_isDisabled": false,
							"contact_information_isDisabled": false,
							"appointment_date_isDisabled": false,
							"appointment_status_isDisabled": false,
							"appointment_remarks_isDisabled": false,
					},

															// 用户列表
				list_user_business_user: [],
														// 用户列表
				list_user_member_user: [],
																		// 预约状态选项列表
				list_appointment_status: ['成功','失败','待安排'],
						
				// ID字段
				field: "trial_appointment_id",

			}
		},
		methods: {
																																																																																																						formatDateValue(value) {
		  const date = new Date(value);
		  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
		},
		async submit(param, func){
			if (!param) {
				param = this.form;
			}
						var pm = this.events("submit_before", Object.assign({}, param)) || param;
			var msg = await this.events("submit_check", pm);
			var ret;
			if (msg) {
				this.$toast(msg, 'danger');
			} else {
																																									ret = this.events("submit_main", pm, func);
			}
			return ret;
		},
		
      /**
       * 提交前验证事件
       * @param {Object} 请求参数
       * @return {String} 验证成功返回null, 失败返回错误提示
       */
            submit_check(param) {
      																																																if (!param.appointment_date){
			return "预约日期不能为空";
		}
															        return null;
      },
			
				
				
						/**
			 * 获取商家用户用户列表
			 */
			async get_list_user_business_user() {
				var json = await this.$get("~/api/user/get_list?user_group=商家用户");
				if(json.result && json.result.list){
					this.list_user_business_user = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
		
				
				
						/**
			 * 获取会员用户用户列表
			 */
			async get_list_user_member_user() {
				var json = await this.$get("~/api/user/get_list?user_group=会员用户");
				if(json.result && json.result.list){
					this.list_user_member_user = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
					async get_user_session_member_user(){
				var _this = this;
				var json = await this.$get("~/api/user_group/get_obj?name=会员用户");
				if(json.result && json.result.obj){
					var source_table = json.result.obj.source_table;
					var user_id = _this.$store.state.user.user_id;
					if (user_id){
						var url = "~/api/"+source_table+"/get_obj?"
						this.$get(url, {"user_id":_this.$store.state.user.user_id}, function(res) {
							if (res.result && res.result.obj) {
								var arr = []
								for (let key in res.result.obj) {
									arr.push(key)
								}
								var arrForm = []
								for (let key in _this.form) {
									arrForm.push(key)
								}
								_this.form["member_user"] = user_id
								_this.disabledObj['member_user' + '_isDisabled'] = true
								for (var i=0;i<arr.length;i++){
                  if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
                    for (var j = 0; j < arrForm.length; j++) {
                      if (arr[i] === arrForm[j]) {
                        if (arr[i] !== "member_user") {
                          _this.form[arrForm[j]] = res.result.obj[arr[i]]
                          _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                          break;
                        }
                      }
                    }
                  }
								}
							}
						});
					}
				}
				else if(json.error){
					console.error(json.error);
				}
			},
	
				
				
				
				
				
		  		get_user_info(name,id){
				var obj = null;
        				  if (name == 'business_user'){
					  obj = this.list_user_business_user.getObj({"user_id":id});
				  }
        				  if (name == 'member_user'){
					  obj = this.list_user_member_user.getObj({"user_id":id});
				  }
            				var ret = "";
				if(obj){
				  ret = obj.nickname+"-"+obj.username;
				}
				return ret;
			},
			/**
			 * 修改文件
			 * @param { Object } files 上传文件对象
			 * @param { String } str 表单的属性名
			 */
			change_file(files, str) {
				var form = new FormData();
				form.append("file", files[0]);
				this.$post("~/api/trial_appointment/upload?", form, (res) => {
					if (res.result) {
						this.form[str] = res.result.url;
					} else if (res.error) {
						this.$toast(res.error.message);
					}
				});
			},
			
			/**
			 * 修改文件
			 * @param { Object } files 上传文件对象
			 * @param { String } str 表单的属性名
			 */
			change_file_multiple(files, str) {
				let _this = this;
				var form = new FormData();
				for (var i = 0; i < files.length; i++) {
					form.set("file", files[i]);
					_this.$post("~/api/trial_appointment/upload?", form, (res) => {
						if (res.result) {
							if (_this.form[str].length > 0) {
								_this.form[str].push(res.result.url);
							} else {
								_this.form[str] = [res.result.url];
							}
						} else if (res.error) {
							_this.$toast(res.error.message);
						}
					});
				}
			},

			/**
			 * 获取对象后获取缓存表单
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_before(param){
				var form = $.db.get("form");
				// if (form) {
        //   delete(form.examine_state)
        //   delete(form.examine_reply)
        //   this.obj = $.push(this.obj ,form);
				// 	this.form = $.push(this.form ,form);
				// }
				// var arr = []
				// for (let key in form) {
				// 	arr.push(key)
				// }
				// for (var i=0;i<arr.length;i++){
				// 	this.disabledObj[arr[i] + '_isDisabled'] = true
				// }
        if (form) {
          var arr = []
          for (let key in form) {
            arr.push(key)
          }
          var arrForm = []
          for (let key in this.form) {
            arrForm.push(key)
          }
          for (var i=0;i<arr.length;i++){
            if (arr[i]!=='examine_state' && arr[i]!=='examine_reply') {
              for (var j = 0; j < arrForm.length; j++) {
                if (arrForm[j] === arr[i]) {
                  this.form[arrForm[j]] = form[arr[i]]
                  this.obj[arrForm[j]] = form[arr[i]]
                  this.disabledObj[arrForm[j] + '_isDisabled'] = true
                  break;
                }
              }
			  if(arr[i] === "source_table"){
			  	this.form['source_table'] = form[arr[i]]
			  }
			  if(arr[i] === "source_id"){
			  	this.form['source_id'] = form[arr[i]]
			  }
			  if(arr[i] === "source_user_id"){
			  	this.form['source_user_id'] = form[arr[i]]
			  }
            }
          }
        }
																													        if (this.form["appointment_date"] && JSON.stringify(this.form["appointment_date"]).indexOf("-")===-1) {
          this.form["appointment_date"] = this.$toTime(parseInt(this.form["appointment_date"]), "yyyy-MM-dd")
        }
								
        $.db.del("form");
				return param;
			},

			/**
			 * 获取对象后获取缓存表单
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json ,func){
				// var form = $.db.get("form");
				// var obj = Object.assign({} ,form ,this.obj);
				// if (obj) {
        //   delete(obj.examine_state)
        //   delete(obj.examine_reply)
				// 	this.obj = $.push(this.obj ,obj);
				// }
				// if (form) {
        //   delete(form.examine_state)
        //   delete(form.examine_reply)
				// 	this.form = $.push(this.form ,form);
				// }

				if(func){
					func(json);
				}
			},

		},
		created() {
															this.get_list_user_business_user();
												this.get_user_session_member_user();
					this.get_list_user_member_user();
																		},
	}
</script>

<style>
	.diy_compute{
		line-height: 40px;
	}
	.diy_field.diy_img_multiple{
		margin: 0;
	}
	.diy_field.diy_img_multiple div{
		float: left;
		position: relative;
		margin: 0 10px 10px 0;
	}
	.diy_field.diy_img_multiple img{
		height: 100px;
		width: auto;
	}
	.diy_field.diy_img_multiple span{
		position: absolute;
		top: 5px;
		right: 5px;
		width: 20px;
		height: 20px;
		background: #0000008a;
		color: #fff;
		line-height: 20px;
		text-align: center;
		border-radius: 100%;
		cursor: pointer;
	}



</style>
